﻿#include "appconfig.h"
#include <QFile>
#include <QSettings>
#include <QDebug>
#include <QApplication>

QString AppConfig::ConfigFile = "config.ini";

bool AppConfig::bLogMsg = true;

QString AppConfig::strNova_ip = "192.168.1.232";
QString AppConfig::strNova_sn = "2YHA23418W4A10023200";
QString AppConfig::strNova_pwd = "SN2008@+";
QString AppConfig::strNova_Prog_identifier[2] = {0};

QString AppConfig::DivIP = "192.168.1.100";       //Div 区域控制卡
int AppConfig::DivPort = 2000;           //

QString AppConfig::ClockIP = "192.168.1.101";         //Timer 区域控制卡
int AppConfig::ClockPort = 2000;           //

QString AppConfig::ScoreIP = "192.168.1.102";         //Score 区域控制卡
int AppConfig::ScorePort = 2000;           //"127.0.0.1";  //

QString AppConfig::VisitorTeamIP = "192.168.1.103";         //Div 区域控制卡
int AppConfig::VisitorTeamPort = 2000;           //

int AppConfig::RegularTime = 90;           //
int AppConfig::ExtTime = 6;           //

int AppConfig::HomeScore = 0;           //
int AppConfig::VisitorScore = 0;           //

QString AppConfig::League_Group = "D";         //
int AppConfig::League_Level = 1;         //

//:/image/miniIcon/mute-off.png
QString AppConfig::HomeIconFile = ":/image/HomeICON.png";         //

int AppConfig::VisitorEffect = 0;         //
int AppConfig::VisitorSpeed = 3;         //
QString AppConfig::VisitorTeamName = "MARSEILL";         //

QString AppConfig::strMediaFilePath = "";
QString AppConfig::strNovaPath = "";

QString AppConfig::strLanguage = "English"; //2024-03-02

void AppConfig::readConfig()
{
    AppConfig::ConfigFile = QApplication::applicationDirPath() +"/config.ini";
    qDebug() << "readConfig-->ConfigFile: " << AppConfig::ConfigFile;

//    if (!checkConfig()) {
//        qDebug() << "readConfig-->checkConfig is null";
//        return;
//    }

    QSettings set(AppConfig::ConfigFile, QSettings::IniFormat);

    set.beginGroup("System");

    AppConfig::strLanguage = set.value("Language").toString();

    qDebug() << "readConfig-->Language: " << AppConfig::strLanguage;

    AppConfig::DivIP = set.value("DivIP").toString();
//    App::DivPort = set.value("DivPort").toInt();

    AppConfig::ClockIP = set.value("ClockIP").toString();
//    App::ClockPort = set.value("ClockPort").toInt();

    AppConfig::ScoreIP = set.value("ScoreIP").toString();
//    App::ScorePort = set.value("ScorePort").toInt();

    AppConfig::VisitorTeamIP = set.value("VisitorTeamIP").toString();
//    App::VisitingTeamPort = set.value("VisitorTeamPort").toInt();

    AppConfig::RegularTime = set.value("RegularTime").toInt();
    AppConfig::ExtTime = set.value("ExtTime").toInt();
    AppConfig::HomeScore = set.value("HomeScore").toInt();
    AppConfig::VisitorScore = set.value("VisitorScore").toInt();

    AppConfig::League_Group = set.value("Leagure_Group").toString();         //
    AppConfig::League_Level = set.value("Leagure_Level").toInt();         //

    AppConfig::HomeIconFile = set.value("HomeIconFile").toString();

    AppConfig::VisitorEffect = set.value("VisitorEffect").toInt();
    AppConfig::VisitorSpeed = set.value("VisitorSpeed").toInt();
    AppConfig::VisitorTeamName = set.value("VisitorTeamName").toString();

    AppConfig::strNova_ip = set.value("Nova_ip").toString();
    AppConfig::strNova_sn = set.value("Nova_sn").toString();
    AppConfig::strNova_pwd = set.value("Nova_pwd").toString();

    if(AppConfig::strNova_pwd.isEmpty())
        AppConfig::strNova_pwd = "SN2008@+";
    AppConfig::strNova_Prog_identifier[0] = set.value("Prog_identifier_0").toString();
    AppConfig::strNova_Prog_identifier[1] = set.value("Prog_identifier_1").toString();

    set.endGroup();
}

void AppConfig::writeConfig()
{
    qDebug() << "writeConfig-->ConfigFile:" << AppConfig::ConfigFile;
    QSettings set(AppConfig::ConfigFile, QSettings::IniFormat);

    set.beginGroup("System");
    set.setValue("Language", AppConfig::strLanguage);
    qDebug() << "writeConfig-->Language: " << AppConfig::strLanguage;

    set.setValue("DivIP", AppConfig::DivIP);
//    set.setValue("DivPort", App::DivPort);

    set.setValue("ClockIP", AppConfig::ClockIP);
//    set.setValue("ClockPort", App::ClockPort);

    set.setValue("ScoreIP", AppConfig::ScoreIP);
//    set.setValue("ScorePort", App::ScorePort);

    set.setValue("VisitorTeamIP", AppConfig::VisitorTeamIP);
//    set.setValue("VisitingTeamPort", App::VisitingTeamPort);

    set.setValue("RegularTime", AppConfig::RegularTime);
    set.setValue("ExtTime", AppConfig::ExtTime);
    set.setValue("HomeScore", AppConfig::HomeScore);
    set.setValue("VisitorScore", AppConfig::VisitorScore);

    set.setValue("Leagure_Group", AppConfig::League_Group);
    set.setValue("Leagure_Level", AppConfig::League_Level);

    set.setValue("HomeIconFile", AppConfig::HomeIconFile);

    set.setValue("VisitorEffect", AppConfig::VisitorEffect);
    set.setValue("VisitorSpeed", AppConfig::VisitorSpeed);
    set.setValue("VisitorTeamName", AppConfig::VisitorTeamName);

    set.setValue("Nova_ip", AppConfig::strNova_ip);
    set.setValue("Nova_sn", AppConfig::strNova_sn);
    set.setValue("Nova_pwd", AppConfig::strNova_pwd);

    set.setValue("Prog_identifier_0", AppConfig::strNova_Prog_identifier[0]);
    set.setValue("Prog_identifier_1", AppConfig::strNova_Prog_identifier[1]);

    set.endGroup();
}

void AppConfig::newConfig()
{
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#endif
    writeConfig();
}

bool AppConfig::checkConfig()
{
    qDebug() << "checkConfig-->ConfigFile:" << AppConfig::ConfigFile;
    //如果配置文件大小为0,则以初始值继续运行,并生成配置文件
    QFile file(AppConfig::ConfigFile);
//    if (file.size() == 0) {
//        newConfig();
//        return false;
//    }

    //如果配置文件不完整,则以初始值继续运行,并生成配置文件
    if (file.open(QFile::ReadOnly)) {
        bool ok = true;
        while (!file.atEnd()) {
            QString line = file.readLine();
            line = line.replace("\r", "");
            line = line.replace("\n", "");
            QStringList list = line.split("=");

            if (list.count() == 2) {
                if (list.at(1) == "") {
                    ok = false;
                    break;
                }
            }
        }

        if (!ok) {
            newConfig();
            return false;
        }
    } else {
        newConfig();
        return false;
    }

    return true;
}
